/*
  listing for function 3C project 2 IMT2272
  aircondition
 */
//////////////// ADCON{0,1,2} REGISTER SETUP //
                                                     //??

//////////////// GLOBALS ///////////////
const int WANTEDTEMP = 20; // wanted temperature
const int DELAYTIMEBZ = 1; // time to delay when buzzing
//////////////// PROTOTYPES ////////////
void buzz(); // make the buzzer buzz once
void delay(uns24 time);

//////////////// ENTRYPOINT ////////////
void main() {
    bool intemprange = 0; // bool to check if temp moved into or out of wanted temp. range

    // configure IO
    TRISC = 0; // all PORTC's are outputs for buzzer
    TRISB = 0; // all PORTB's are outputs for LED's

    // configure ADCON{0,1,2}
    ADCON0 = 0x01; // see lecture notes for
    ADCON1 = 0x01; // description
    ADCON2 = 0x80; // 

    while(1) { // run loop
	ADCON0.1 = 0;
	if (ADCON0.1< WANTEDTEMP-1 || ADCON0.1 > WANTEDTEMP+1) { // check if temp is outside of wanted range
	    PORTB.0 = 1; // turn on light/keep lit
	    if (intemprange == 1) {
		buzz(); // make sound
		intemprange = 0; // set bool to false
	    }
	} else { // if it is inside wanted range
	    PORTB.0 = 0; // turn off light/keep unlit
	    if (intemprange == 0) {
		buzz(); // make sound
		intemprange = 1; // set bool to true
	    } // if (intemprange) end
	} // if (temp <> WANTEDTEMP) end
    } // while (1) run loop end
} // program end

void buzz() {
    PORTC.2 = 1; // start buzzing
    delay(DELAYTIMEBZ*10000); // wait/delay for 'DELAYTIMEBZ' seconds
    PORTC.2 = 0; // stop buzzing
}

void delay(uns24 time) {
    uns16 i;
    for (i=0;i<time;)
    {
	i++;
    }
}
